﻿Imports System.Reflection
Imports System.Web.UI
Imports System.Security
Imports System.Web


Namespace AboutDNN.DNNMVC


    Public Class BaseController

        Public Function ViewResult() As ActionResult
            Return ViewResult(App.MVCContext.Route.Controller, App.MVCContext.Route.Action, Nothing)
        End Function

        Public Function ViewResult(ByVal data As Object) As ActionResult
            Return ViewResult(App.MVCContext.Route.Controller, App.MVCContext.Route.Action, data)
        End Function

        Public Function ViewResult(ByVal viewName As String) As ActionResult
            Return ViewResult(App.MVCContext.Route.Controller, viewName, Nothing)
        End Function

        Public Function ViewResult(ByVal viewName As String, ByVal Data As Object) As ActionResult
            Return ViewResult(App.MVCContext.Route.Controller, viewName, Data)
        End Function

        Public Function ViewResult(ByVal Controller As String, ByVal Action As String, ByVal data As Object) As ActionResult
            Return New ActionResult(data, ActionResult.ActionTypeEnum.View, New RouteInfo() With {.Action = Action, .Controller = Controller})
        End Function

        Public Function JsonResult(ByVal jsondata As String) As ActionResult
            Return New ActionResult(jsondata, ActionResult.ActionTypeEnum.Json)
        End Function

        Public Function LiteralResult(ByVal data As String) As ActionResult
            Return New ActionResult(data, ActionResult.ActionTypeEnum.Literal)
        End Function

        Public Function RedirectToAction(ByVal actionName As String) As ActionResult
            Return RouteHelper.ExecuteAction(App.MVCContext.Route.Controller, actionName)
        End Function

        Public Function RedirectToAction(ByVal controllerName As String, ByVal actionName As String) As ActionResult
            Return RouteHelper.ExecuteAction(controllerName, actionName)
        End Function

        Public ReadOnly Property IsPOST() As Boolean
            Get
                Return App.MVCContext.RequestType = "POST"
            End Get
        End Property

        Public ReadOnly Property IsGET() As Boolean
            Get
                Return App.MVCContext.RequestType = "GET"
            End Get
        End Property

        Public ReadOnly Property IsAuthenticated() As Boolean
            Get
                Return HttpContext.Current.Request.IsAuthenticated
            End Get
        End Property

        Public Sub AuthenticatedOnly()
            If IsAuthenticated = False Then
                Throw New SecurityException("Must be logged in to perform this action!")
            End If
        End Sub

        ''' <summary>
        ''' Makes sure the request is a post. Throws an exception if it is not
        ''' </summary>
        Public Sub POSTRequestOnly()
            If IsPOST = False Then
                App.HandleException(New ApplicationException("Invalid GET request!"))
            End If
        End Sub

        ''' <summary>
        ''' Makes sure the request is a get. Throws an exception if it is not
        ''' </summary>
        Public Sub GETRequestOnly()
            If IsGET = False Then
                App.HandleException(New ApplicationException("Invalid POST request!"))
            End If
        End Sub

        Public Function GetValueOrError(ByVal name As String) As String
            Return App.GetValueOrError(name)
        End Function

        Public Function GetValueOrErrorAsInt(ByVal name As String) As String
            App.RequireValue(name)
            Return App.GetInt(name)
        End Function

        Public Function GetValueOrErrorAsDouble(ByVal name As String) As String
            App.RequireValue(name)
            Return App.GetDouble(name)
        End Function

        Public Function GetValueOrErrorAsDate(ByVal name As String) As String
            App.RequireValue(name)
            Return App.GetDate(name)
        End Function

        Public Shared Function GetInt(ByVal key As String) As Integer?
            Return App.GetInt(key)
        End Function

        Public Shared Function GetIntArray(ByVal key As String) As List(Of Integer)
            Return App.GetIntArray(key)
        End Function

        Public Shared Function GetDecimal(ByVal key As String) As Decimal?
            Return App.GetDecimal(key)
        End Function

        Public Shared Function GetDouble(ByVal key As String) As Double?
            Return App.GetDouble(key)
        End Function

        Public Shared Function GetDate(ByVal key As String) As Date?
            Return App.GetDate(key)
        End Function

        Public Shared Function GetBool(ByVal key As String) As Boolean
            Return App.GetBool(key)
        End Function


    End Class

End Namespace